//
//  FoodClassViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/27.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "FoodClassViewController.h"
#import "HX_Macro.h"
#import "HXHeaderCycleCell.h"
#import "HXCycleHeadModel.h"
#import "HXFoodClassTCell.h"
#import "HXFoodClassModel.h"
#import "HXCycleImageVC.h"
#define kHeader @"headerCycle"
#define kFoodClass @"foodClass"
@interface FoodClassViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate>
/**
 *  头视图数组
 */
@property (nonatomic, strong) UIView *headerView;
/**
 *  header滚动视图
 */
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *cycleHeader;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *foodClass;
@end

@implementation FoodClassViewController
- (NSMutableArray *)foodClass {
    if (!_foodClass) {
        _foodClass = [NSMutableArray array];
    }
    return _foodClass;
}
- (NSMutableArray *)cycleHeader {
    if (!_cycleHeader) {
        _cycleHeader = [NSMutableArray array];
    }
    return _cycleHeader;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLORAUTO;
    [self getNetWorking];
    [self getNetWokingFoodClass];
    [self creatHeaderView];
    [self creatHeaderCollectionView];
    [self creatTableView];
}
- (void)creatHeaderCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 1;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _headerView.HX_height) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"HXHeaderCycleCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHeader];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - _headerView.HX_height - 108) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    [self.view addSubview:_tableView];
    MJRefreshAutoNormalFooter *foot = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addData)];
    [foot setTitle:@"已显示全部" forState:MJRefreshStateIdle];
    self.tableView.mj_footer = foot;
    [_tableView registerNib:[UINib nibWithNibName:@"HXFoodClassTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kFoodClass];
}
- (void)addData {
    static int index = 2;
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"user_id=1666223&page=%d&token=88E92FD7679C76FA91A54FE14641AD60&methodName=CourseIndex&size=20&version=4.40&", index];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
            [self.foodClass addObject:[HXFoodClassModel ModelWithDic:dic]];
        }
        [self.tableView.mj_footer endRefreshing];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
    index++;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _cycleHeader.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXHeaderCycleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHeader forIndexPath:indexPath];
    HXCycleHeadModel *model = self.cycleHeader[indexPath.row];
    [cell.cycleImage sd_setImageWithURL:[NSURL URLWithString:model.album_logo]];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _foodClass.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXFoodClassTCell *cell = [tableView dequeueReusableCellWithIdentifier:kFoodClass];
    cell.backgroundColor = [UIColor whiteColor];
    cell.foodClassModel = self.foodClass[indexPath.row];
    return cell;
}
- (void)creatHeaderView {
    self.headerView = [[UIView alloc] init];
    _headerView.backgroundColor = [UIColor whiteColor];
    _headerView.HX_height = 64;
    _headerView.HX_width = SCREEN_WIDTH;
    [self.view addSubview:_headerView];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.collectionView.HX_width / 5.3, self.collectionView.HX_height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (void)getNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"--02e90489-6d31-45f1-b595-b70607b0301c\nContent-Disposition: form-data; name=methodName\nContent-Length: 10\n\nCourseLogo\n--02e90489-6d31-45f1-b595-b70607b0301c--";
    NSDictionary *dicHeader = @{@"Content-Type" : @"multipart/form-data; boundary=02e90489-6d31-45f1-b595-b70607b0301c"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSLog(@"%@", result);
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *albums = dicData[@"albums"];
        for (NSDictionary *dicAlbums in albums) {
            [self.cycleHeader addObject:[HXCycleHeadModel ModelWithDic:dicAlbums]];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)getNetWokingFoodClass {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"user_id=1666223&page=1&token=88E92FD7679C76FA91A54FE14641AD60&methodName=CourseIndex&size=20&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
            [self.foodClass addObject:[HXFoodClassModel ModelWithDic:dic]];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) { 
    }];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HXCycleImageVC *cycle = [[HXCycleImageVC alloc] init];
    HXCycleHeadModel *classModel = self.cycleHeader[indexPath.row];
    cycle.ID = classModel.series_id;
    [self.navigationController pushViewController:cycle animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HXCycleImageVC *hxImage = [[HXCycleImageVC alloc] init];
    HXFoodClassModel *foodModel = self.foodClass[indexPath.row];

    hxImage.ID = [NSString stringWithFormat:@"%ld", foodModel.series_id];
    [self.navigationController pushViewController:hxImage animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
